package org.jeecg.common.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.entity.SysDepart;
import org.jeecg.common.entity.SysDepartTreeModel;
import org.jeecg.common.entity.SysPosition;
import org.jeecg.common.service.ISysDepartService;
import org.jeecg.common.service.ISysPositionService;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;


/**
 * <p>
 * 部门表 前端控制器
 * <p>
 *
 * @Author: Steve @Since： 2019-01-22
 */
@RestController
@RequestMapping("/erp/sysDepart")
@Slf4j
public class SysDepartController {

    @Autowired
    private ISysDepartService sysDepartService;

    @Autowired
    private ISysPositionService positionService;

    /**
     * 异步查询部门list
     *
     * @param parentId 父节点 异步加载时传递
     * @param ids      前端回显是传递
     * @return
     */
    @RequestMapping(value = "/queryDepartTreeSync", method = RequestMethod.GET)
    public Result<List<SysDepartTreeModel>> queryDepartTreeSync(
            @RequestParam(name = "pid", required = false) String parentId,
            @RequestParam(name = "ids", required = false) String ids,
            @RequestParam(name = "pcode", required = false) String parentCode) {
        Result<List<SysDepartTreeModel>> result = new Result<>();
        try {
            List<SysDepartTreeModel> list;
            //传进来的不为空并且父级不等于65
//            parentCode="65";
            if (oConvertUtils.isNotEmpty(parentCode) && !parentCode.equals("65")) {
                LambdaQueryWrapper<SysDepart> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(SysDepart::getDelFlag, CommonConstant.DEL_FLAG_0);
                wrapper.eq(SysDepart::getOrgCode, parentCode);
                SysDepart one = sysDepartService.getOne(wrapper);
                if (null != one) parentId = one.getId();
                list = sysDepartService.queryTreeListByPidWithSelf(parentId, ids);
            } else {
                if (oConvertUtils.isEmpty(parentId)) {
                    LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                    String positionId = sysUser.getPosition();
                    SysPosition sysPosition = positionService.getById(positionId);
                    LambdaQueryWrapper<SysDepart> wrapper = new LambdaQueryWrapper<SysDepart>();
//                    wrapper.eq(SysDepart::getOrgCode, sysPosition.getBelongOrg());
                    //业务中台查询组织机构权限字段由org_code改为data_org_code】
                    wrapper.eq(SysDepart::getOrgCode, sysPosition.getDataOrgCode());
                    wrapper.eq(SysDepart::getDelFlag, CommonConstant.DEL_FLAG_0);
                    SysDepart one = sysDepartService.getOne(wrapper);
                    parentId = one.getId();
                    list = sysDepartService.queryTreeListByPidWithSelf(parentId, ids);
                } else {
                    list = sysDepartService.queryTreeListByPid(parentId, ids);

                }

            }
            result.setResult(list);
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return result;
    }




}
